package com.wf.course.web.controller.course;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.course.CourseWeixin;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICourseWeixinService;
import com.wf.course.web.Interceptor.LogInfo;
import com.wf.course.service.ICourseInfoService;
/**
 * 
 * @content 课程微信Controller
 */
@RequestMapping("courseWeixin")
@Controller
public class CourseWeixinController {

	@Autowired
	private ICourseWeixinService wxService;
	@Autowired
	private ICourseInfoService courseInfoService;

	/**
	 * 课程微信列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param courseName
	 * @param wxName
	 * @param model
	 * @return
	 */
	@LogInfo("查询课程微信信息列表")
	@RequestMapping("courseWeixinList")
	public String chapterList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "courseName", required = false) String courseName, 
			@RequestParam(value = "wxName", required = false) String wxName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("courseName", courseName);
		cons.put("wxName", wxName);
		PageBean<CourseWeixin> pageBean = new PageBean<CourseWeixin>(pageNo, pageSize, null, cons);
		pageBean.setUrl("CourseWeixin/chapterList");
		this.wxService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "course/courseWeixinList";
	}

	/**
	 * 课程 修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改课程微信信息，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			CourseWeixin courseWeixin = wxService.queryCourseWeixinById(id);
			model.addAttribute("courseWeixin", courseWeixin);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		
		return "course/courseWeixinEdit";
	}

	/**
	 * 新增和修改
	 * @param CourseWeixin
	 * @param weixinFile
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改课程微信信息")
	@RequestMapping(value = "courseWeixinEdit" , produces = "text/html;charset=UTF-8")
	public String courseWeixinEdit(@ModelAttribute CourseWeixin courseWeixin, MultipartFile weixinFile,HttpServletRequest request) {
		
		wxService.saveOrUpdateCourseWeixin(courseWeixin, weixinFile);
		
		return "redirect:/courseWeixin/courseWeixinList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除课程微信信息")
	@RequestMapping(value = "remove/{id}")
	public String removeCourseWeixin(@PathVariable("id") Integer id) {
		
		wxService.deleteById(id);
		
		return "redirect:/courseWeixin/courseWeixinList";
	}
	

}
